﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataLayer.EF;
using EntityLayer;

namespace DataLayer.CLASES_DAO
{
    public class clsT_ContactoDAO
    {
        private BD_GESTIONVISITAEntities ContextoDeObjs = new BD_GESTIONVISITAEntities();
        public Boolean Insertar(T_Contacto obj)
        {
            try
            {
                ContextoDeObjs.AddObject("T_Contacto", obj);
                ContextoDeObjs.SaveChanges();
                return true;
            }
            catch (Exception)
            {
                throw;
            }
        }
        public Boolean Modificar(T_Contacto obj)
        {
            try
            {
                ContextoDeObjs.AttachTo("T_Contacto", obj);
                ContextoDeObjs.ObjectStateManager.ChangeObjectState(obj, System.Data.EntityState.Modified);
                ContextoDeObjs.SaveChanges();
                return true;
            }
            catch (Exception)
            {
                throw;
            }
        }
        public Boolean Eliminar(T_Contacto obj)
        {
            try
            {
                ContextoDeObjs.AttachTo("T_Contacto", obj);
                ContextoDeObjs.DeleteObject(obj);
                ContextoDeObjs.SaveChanges();
                return true;
            }
            catch (Exception)
            {
                throw;
            }
        }
        public List<T_Contacto> listar(int? co_contacto = null)
        {
            List<T_Contacto> lista = new List<T_Contacto>();
            try
            {
                var q = from c in ContextoDeObjs.T_Contacto
                        select c;

                if (co_contacto != null) q = q.Where(f1 => f1.co_contacto == co_contacto);

                lista = q.ToList();
            }
            catch (Exception)
            {
                lista = null;
                throw;
            }
            return lista;
        }
        public List<T_Contacto> ListarxEmpresa(Int64? nu_ruc_cliente)
        {
            List<T_Contacto> lista = new List<T_Contacto>();
            try
            {
                var q = from c in ContextoDeObjs.T_Contacto
                        where c.nu_ruc_cliente == nu_ruc_cliente
                        select c;

                lista = q.ToList();
            }
            catch (Exception)
            {
                lista = null;
                throw;
            }
            return lista;
        }
        public int? ObtenerUltimoIDContacto()
        {
            int? ultimo = 0;
            try
            {
                var q = from t in
                            (from t in ContextoDeObjs.T_Contacto
                             select new
                             {
                                 t.co_contacto,
                                 Dummy = "x"
                             })
                        group t by new { t.Dummy } into g
                        select new
                        {
                            ultimo = (Int32?)g.Max(p => p.co_contacto)
                        };

                if (q.ToList() == null || q.ToList().Count() == 0) { ultimo = 1; }
                else { ultimo = q.ToList()[0].ultimo + 1; }
            }
            catch (Exception)
            {
                ultimo = 0;
                throw;
            }

            return ultimo;
        }
    }
}
